package net.kts.placebook.tips;

import java.util.ArrayList;

import net.kts.main.R;
import net.kts.placebook.dao.PlacebookDao;
import net.kts.placebook.gallery.GalleryList;
import net.kts.placebook.session.UMISSession;
import net.kts.transrate.TranslateService;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class TipsList extends ListActivity {
	
	public static final String tagName = "fs333";
	private PlacebookDao dao;
	private ProgressDialog mProgress;
	private TipsAdapter tipsAdapter;
	private ArrayList<Tips> fsTipList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 레이아웃 레퍼런스
		setContentView(R.layout.pb_venue_info);
		
        mProgress	= new ProgressDialog(this);
		fsTipList	= new ArrayList<Tips>();
		tipsAdapter = new TipsAdapter(this);
        dao 		= new PlacebookDao(this);
		
		// 이전 액티비티로부터 데이터 추출
		// 1. venum ID 추출
		Intent intent = getIntent();
		String venueId = intent.getStringExtra("venueId");
		String venueName = intent.getStringExtra("name");
		String address = intent.getStringExtra("address");
		
		Log.i(tagName, "VenueID : " + venueId);
		
		// 2. 유효성 검사
		if(venueId.equals("") || venueId == null){
			String guideId = "장소의 아이디가 없습니다. 다시 요청해주세요.";
			Toast.makeText(TipsList.this, TranslateService.translate(guideId), Toast.LENGTH_SHORT).show();
			finish();
		}
		if(venueName.equals("") || venueName == null){
			String guideName = "장소명이 없습니다.";
			Toast.makeText(TipsList.this, TranslateService.translate(guideName), Toast.LENGTH_SHORT).show();
			finish();
		}
		
		String otherVenueName = TranslateService.translate(venueName);
		String otherAddress = null;
		// 주소가 있으면 번역
		if(address != null){
			otherAddress = TranslateService.translate(address);
		}

		// 장소 아이디 UMISSession에 저장
		UMISSession.getInstance().setObjectByKey("venueId", venueId);
		
		// 사진 버튼 이벤트 리스너
		Button btn = (Button)findViewById(R.id.getPhoto);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TipsList.this, GalleryList.class);
				
				String id = (String) UMISSession.getInstance().getObjectByKey("venueId");
				Log.i(tagName, "::사진버튼리스너에 장소 아이디 : " + id);
				
				intent.putExtra("venueId", id);
				
				startActivity(intent);
			}
		});
		
		// 4. 장소 명 출력
		TextView tvVenueLabel = (TextView) findViewById(R.id.tv_venueLabel);
		TextView tvVenueAddress = (TextView) findViewById(R.id.tv_address);
		TextView tvOtherVenueLabel = (TextView) findViewById(R.id.tv_other_venueLabel);
		TextView tvOtherAddress = (TextView) findViewById(R.id.tv_other_address);
		
		tvVenueLabel.setText(venueName);
		tvOtherVenueLabel.setText(otherVenueName);

		// 주소가 있으면 출력
		if(address != null){
			tvVenueAddress.setText(address);
			tvOtherAddress.setText(otherAddress);
		}
		
		// 3. 팁 리스트 출력
		loadTips(venueId);
	}
	
    private void loadTips(final String venueId) {
		String wait = TranslateService.translateRepository("잠시만 기다려주세요.");
		if(wait == null){
			mProgress.setMessage("Please wait a moment.");
		}else if(wait.trim().equals("")){
			mProgress.setMessage("Please wait a moment.");
		}else{
			mProgress.setMessage(wait);
		}
    	mProgress.show();
    	
    	new Thread() {
    		@Override
    		public void run() {
    			int what = 0;
    			
    			try {
    				fsTipList = dao.getTips(venueId);
    			} catch (Exception e) {
    				Log.i(tagName, "loadTips Exception");
    				what = 1;
    				e.printStackTrace();
    			}
    			
    			mHandler.sendMessage(mHandler.obtainMessage(what));
    		}
    	}.start();
    }
	
    private Handler mHandler = new Handler() {
    	@Override
    	public void handleMessage(Message msg) {
    		mProgress.dismiss();
    		
    		Log.i(tagName, msg.toString());
    		
    		if (msg.what == 0) {
    			if (fsTipList.size() == 0) {
    				Toast.makeText(TipsList.this, "No Tips available", Toast.LENGTH_SHORT).show();
    				return;
    			}
    			
    			tipsAdapter.setData(fsTipList);    			
    			setListAdapter(tipsAdapter);
    			
    		} else {
    			Toast.makeText(TipsList.this, "Failed to load Tips", Toast.LENGTH_SHORT).show();
    		}
    	}
    };



}
